﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.DAL.Repository;
using CMS.BL.Entity;
using CMS.Utils.Cryptography;
using CMS.Utils.Diagnostics;
using CMS.BL;

namespace CMS.Services
{
    public class User : ServiceBase, IUser
    {
        private IAdminUserRepository AdminUserRepository { get; set; }
        private IUserGroupsRepository UserGroupRepository { get; set; }
        private IDomainsRepository DomainsRepository { get; set; }

        public User()
            : base()
        {
            this.AdminUserRepository = new AdminUserRepository(this.Context);
            this.UserGroupRepository = new UserGroupsRepository(this.Context);
            this.DomainsRepository = new DomainsRepository(this.Context);
        }

        #region AdminUsers

        public bool CanUseUserEmail(string email, int currentUserID = 0)
        {
            var user = this.GetUserByEmail(email);
            return (user != null && user.AdminUserID != currentUserID) ? false : true;
        }

        public List<tbl_AdminUsers> GetAllUsers()
        {
            return AdminUserRepository.GetAll().ToList();
        }

        public List<string> GetPermissionsByUserID(int userID)
        {
            return AdminUserRepository.GetPermissionsByID(userID);
        }

        public tbl_AdminUsers GetUserByEmail(string email)
        {
            return AdminUserRepository.GetByEmail(email);
        }

        public tbl_AdminUsers GetUserByEmailAndPassword(string email, string password)
        {
            return AdminUserRepository.GetByEmailAndPassword(email, Sha512.GetSHA512Hash(password));
        }

        public tbl_AdminUsers GetUserByID(int userID)
        {
            return AdminUserRepository.GetByID(userID);
        }

        public int GetUserGroupIDByUserID(int userID)
        {
            tbl_AdminUsers user = AdminUserRepository.GetByID(userID);
            return (user != null) ? user.US_UserGroupID : 0;
        }

        public bool DeleteUser(int userID)
        {
            return AdminUserRepository.DeleteUser(userID);
        }

        public tbl_AdminUsers SaveUser(string email, string userName, string password, int groupID, int userID)
        {
            if (userID == 0 && String.IsNullOrEmpty(password))
                return null;

            password = String.IsNullOrEmpty(password) ? String.Empty : Sha512.GetSHA512Hash(password);
            return AdminUserRepository.SaveUser(email, userName, password, groupID, userID);
        }

        #endregion


        #region User Groups

        public List<tbl_UserGroups> GetAllUserGroups()
        {
            return UserGroupRepository.GetAll().ToList();
        }

        public List<tbl_UserGroups> GetAllUserGroupsOrdered()
        {
            return UserGroupRepository.GetAll().OrderBy(ug => ug.UG_Order).ToList();
        }

        public tbl_UserGroups GetUserGroupByID(int groupID)
        {
            return UserGroupRepository.GetByID(groupID);
        }

        public int GetUsersAmountForUserGroup(int groupID)
        {
            var group = UserGroupRepository.GetByID(groupID);
            return (group != null) ? 
                group.tbl_AdminUsers.Count : 
                0;
        }

        public bool DeleteUserGroup(int groupID)
        {
            return UserGroupRepository.DeleteUserGroup(groupID);
        }

        public tbl_UserGroups SaveUserGroup(string groupName, int[] menuItems, int[] permissions, int groupID)
        {
            return (String.IsNullOrEmpty(groupName)) ?
                null :
                UserGroupRepository.SaveUserGroup(groupName, menuItems, permissions, groupID);
        }

        #endregion
    }
}
